package com.example.cp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import java.util.List;

public class UserInfoProvider extends ContentProvider {

    UserHelper userHelper;

    public UserInfoProvider() {

    }

    /**
     * 删除
     * @param uri
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int i = userHelper.deleteUser(selection,selectionArgs);
        return i;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 增加
     * @param uri
     * @param values
     * @return
     */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        UserInfo userInfo = new UserInfo();
        userInfo.setName(values.getAsString("name"));
        userInfo.setAge(values.getAsInteger("age"));
        userInfo.setAddr(values.getAsString("addr"));
        userInfo.setSex(values.getAsString("sex"));
        long l = userHelper.insertUser(userInfo);
        return uri;
    }

    @Override
    public boolean onCreate() {
        if(userHelper == null) userHelper = new UserHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor cursor = userHelper.queryCursor();
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        UserInfo userInfo = new UserInfo();
        userInfo.setName(values.getAsString("name"));
        userInfo.setAge(values.getAsInteger("age"));
        userInfo.setAddr(values.getAsString("addr"));
        userInfo.setSex(values.getAsString("sex"));
        int i = userHelper.updateUser(userInfo);
        return i;
    }
}